package com.mingliqiye.minecraft.enchantment.conflict.common;

import static com.mingliqiye.minecraft.enchantment.conflict.common.DataFormat.JSONOBJECTMAPPER;
import static com.mingliqiye.minecraft.enchantment.conflict.common.DataFormat.YALMOBJECTMAPPER;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

public class DefConfig {

    private static final String DEFCONFIGPATH = "/assets/enchantmentdoesnotconflict/config/";
    private static final String DEFCONFIGV121PATH = DEFCONFIGPATH + "enchantmentdoesnotconflict_1.21.json";
    private static final String DEFCONFIGV120PATH = DEFCONFIGPATH + "enchantmentdoesnotconflict_1.20.json";
    private final Map<String, ConfigPair> config121;
    private final Map<String, ConfigPair> config120;
    private final VersionsEnum versionsEnum;

    public static String getResByString(String path) {
        try (InputStream i = DefConfig.class.getResourceAsStream(path)) {
            byte[] data = i.readAllBytes();
            return new String(data, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public DefConfig(VersionsEnum versionsEnum) {
        try {
            config121 = JSONOBJECTMAPPER.readValue(getResByString(DEFCONFIGV121PATH), ConfigManager.MODCONFIGTYPE);
            config120 = JSONOBJECTMAPPER.readValue(getResByString(DEFCONFIGV120PATH), ConfigManager.MODCONFIGTYPE);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        this.versionsEnum = versionsEnum;
    }

    public Map<String, ConfigPair> getConfig121() {
        return config121;
    }

    public Map<String, ConfigPair> getConfig120() {
        return config120;
    }

    public Map<String, ConfigPair> getConfig() {
        if (versionsEnum == VersionsEnum.V120) {
            return getConfig120();
        } else if (versionsEnum == VersionsEnum.V121) {
            return getConfig121();
        }
        return null;
    }
}
